import { all, postJson, deleteJson, putJson, get } from "@/common/VueAxios"
import type { ApiResult } from "./basemodel"

export interface ProjectDeployer {
    id?: number
    createtime?: string
    project_id?: number
    command?: string
    server_ids?: string
}

export function save(dtos: ProjectDeployer[]) {
    const addDtos = dtos.filter(o => o.id == undefined || o.id == 0)
    const updateDtos = dtos.filter(o => o.id != undefined && o.id > 0)
    const reqs: Promise<ApiResult>[] = []

    if (addDtos.length > 0) {
        reqs.push(create(addDtos))
    }
    if (updateDtos.length > 0) {
        reqs.push(update(updateDtos))
    }
    return all(reqs)
}

export function create(dto: ProjectDeployer[]) {
    return postJson<ProjectDeployer[], ApiResult>("/project/deployer", dto)
}

export function update(dto: ProjectDeployer[]) {
    return putJson<ProjectDeployer[], ApiResult>("/project/deployer", dto)
}

export function removeById(id: number) {
    return remove([id])
}
export function remove(ids?: number[]) {
    return deleteJson<number[] | undefined, ApiResult>("/project/deployer", ids)
}

export function getProjectId(projectId?: number) {
    return get<{ project_id?: number }, ApiResult<ProjectDeployer[]>>("/project/deployer", { project_id: projectId })
}